﻿/*
' Copyright (c) 2017  Rick
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using DotNetNuke;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Services.Localization;
using RickDnn.Modules.DNNTaskManager.Components;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Http;
using DotNetNuke.Web.UI.WebControls;


using System.Web.Http;
using DotNetNuke.Entities.Content.Common;
using DotNetNuke.UI.Containers;
using System.Web.UI;
using DotNetNuke.UI.Utilities;

namespace RickDnn.Modules.DNNTaskManager
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The View class displays the content
    /// 
    /// Typically your view control would be used to display content or functionality in your module.
    /// 
    /// View may be the only control you have in your project depending on the complexity of your module
    /// 
    /// Because the control inherits from DNNTaskManagerModuleBase you have access to any custom properties
    /// defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
    /// 
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class View :  DNNTaskManagerModuleBase, IActionable, IDisposable
    {

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
           
            try
            {
                rptTaskList.DataSource = TaskController.GetTasks(ModuleId);
                rptTaskList.DataBind();
            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        public ModuleActionCollection ModuleActions
        {
            get
            {
                var actions = new ModuleActionCollection
                    {
                        {
                            GetNextActionID(), Localization.GetString("EditModule", LocalResourceFile), "", "", "",
                            EditUrl(), false, SecurityAccessLevel.Edit, true, false
                        }
                    };
                return actions;
            }
        }

        public void rptTaskListOnItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)//check if it is a list item
            {
                //should be pnlAdminControl
                var pnlAdminControls = e.Item.FindControl("pnlAdmin") as Panel;
                var lnkEdit = e.Item.FindControl("lnkEdit") as LinkButton;
                var lnkDelete = e.Item.FindControl("lnkDelete") as LinkButton;
                var lblEd = e.Item.FindControl("lblEd") as Label;
                var curTask = (Task)e.Item.DataItem;
                //IsEditable to control if only can see when eidt mode
                if (IsEditable && pnlAdminControls != null && lnkEdit != null && lnkDelete != null)
                    {

                    pnlAdminControls.Visible = true;
                    lnkDelete.Enabled=lnkEdit.Enabled = lnkDelete.Visible= lnkEdit.Visible = true;
                    //assign parameter for tid of lnlEdit                   
                    lnkDelete.CommandArgument = lnkEdit.CommandArgument = curTask.TaskId.ToString();

                    lblEd.Visible = true;

                    //Add confirm button
                    ClientAPI.AddButtonConfirm(lnkDelete, Localization.GetString("ConfirmDelete", LocalResourceFile));
                }
                else
                {
                    pnlAdminControls.Visible = false;
                }
            }
        }


        public void rptTaskListOnItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                //server side use response
                //tid: taskId
                //when databound will assign e.CommandArgument with taskId, so when redirect will with tid parameter
                Response.Redirect(EditUrl(string.Empty, string.Empty, "Edit", "tid=" + e.CommandArgument));

                //Response.Redirect(EditUrl(string.Empty, string.Empty, "Edit"));
                //Try  overload 5
                //popup true
                //Response.Redirect(EditUrl(TabId,"Edit",true, "tid=" + e.CommandArgument));
            }

            if (e.CommandName == "Delete")
            {
                TaskController.DeleteTask(Convert.ToInt32( e.CommandArgument));
            }
            if (e.CommandName=="Test")
            {
                LinkButton b = source as  LinkButton;
                if (b != null)
                {
                    //Label c = (Label)b.Parent.FindControl("Label1");
                    Label c = (Label)b.FindControl("Label1");
                    if (c != null)
                    {
                        c.Text = "text changed in handler";
                        c.ForeColor = System.Drawing.Color.Green;
                    }
                }
            }
            Response.Redirect(DotNetNuke.Common.Globals.NavigateURL());

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //Response.Redirect(EditUrl(string.Empty, string.Empty, "Edit", "tid=4"));
            //Label1.Text = EditUrl(string.Empty, string.Empty, "Edit", "tid=4");
            //Response.Redirect("http://www.dnnweb.me/DNNTaskManager/ctl/Edit/mid/390/tid/4?popUp=true");
            //Response.Redirect("DNNTaskManager/ctl/Edit/mid/390/tid/4?popUp=true,false, 550, 950, true, ''");
            //Response.Redirect(EditUrl(string.Empty, string.Empty, "File", "tid=4").ToString());

            //javascript:dnnModal.show('http://www.dnnweb.me/DNNTaskManager/ctl/Edit/mid/390/tid/4?popUp=true',/*showReturn*/false, 550, 950, true, '');
            //Response.Redirect("javascript: dnnModal.show('http://www.dnnweb.me/DNNTaskManager/ctl/Edit/mid/390/tid/5?popUp=true',/*showReturn*/false, 550, 950, true, '')");

            //Response.Redirect("javascript: dnnModal.show('http://www.dnnweb.me/DNNTaskManager/ctl/Edit/mid/390/tid/5?popUp=true',/*showReturn*/false, 550, 950, false, '')");
            //Response.Redirect("javascript: dnnModal.show('DNNTaskManager/ctl/Edit/mid/390/tid/5?popUp=true',/*showReturn*/false, 550, 950, true, '')");
            //refuse to display google, because in a frame because it set 'X-Frame-Options' to 'SAMEORIGIN'.
            // Response.Redirect("javascript: dnnModal.show('https://www.google.com?popUp=true',/*showReturn*/false, 550, 950, false, '')");
            //http://www.dnnweb.me/DNNTaskManager?ctl=http://www.dnnweb.me/DNNTaskManager/ctl/Edit/mid/390/tid/5?popUp=true
            //Response.Redirect(DotNetNuke.Common.Globals.NavigateURL("http://www.dnnweb.me/DNNTaskManager/ctl/Edit/mid/390/tid/5?popUp=true"));

            //Response.Redirect(DotNetNuke.Common.Globals.NavigateURL("Edit/mid/390/tid/6?popUp=true"));

            // Response.Redirect(EditUrl("Edit", "tid=6"));
            //good
            // Response.Redirect("https://google.com");
            //http://www.dnnweb.me/DNNTaskManager?ctl=www.google.com
            //Response.Redirect(DotNetNuke.Common.Globals.NavigateURL("www.google.com"));
            //Response.Redirect(DotNetNuke.Common.Globals.NavigateURL());

            //http://www.dnnweb.me/DNNTaskManager/ctl/Edit/mid/7
            //Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(TabId, "Edit", "mid=7"));


            //http://www.dnnweb.me/DNNTaskManager/ctl/Edit/mid/390/tid/7
            //Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(TabId, "Edit", "mid=390","tid=7"));


            // http://www.dnnweb.me/DNNTaskManager/ctl/Edit/mid/390/tid/7?popUp=true no back ground
            //Response.Redirect(DotNetNuke.Common.Globals.NavigateURL(TabId, "Edit", "mid=390", "tid=7","popUp=true"));

            //Response.Redirect(EditUrl(string.Empty, string.Empty, "Edit", "tid=4"),false);
            //Response.Redirect(EditUrl());

            //Label1.Text = DotNetNuke.Common.Globals.NavigateURL(TabId, "Edit", "mid=390", "tid=7").ToString();

        }       
    }
}